//
//  TopicDetailViewController.swift
//  LaiLang
//
//  Created by David on 2017/2/25.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class TopicDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    
    var switchingTitle: UILabel!

    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.groupTableViewBackground
//        let navbar = LLNavBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64))
//        navbar.barTintColor = UIColor.clear
        
        let headView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 200))
        headView.backgroundColor = UIColor.lightGray
        view.addSubview(headView)
        
        let backBtn = UIButton(frame: CGRect(x: 16, y: 28, width: 24, height: 24))
        backBtn.setImage(#imageLiteral(resourceName: "navbar_back"), for: .normal)
        headView.addSubview(backBtn)
        backBtn.addTarget(self, action: #selector(TopicDetailViewController.goBack), for: .touchUpInside)
        
        tableView = UITableView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height-64))
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 200-64))
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.backgroundColor = UIColor.clear
        tableView.delegate = self
        tableView.dataSource = self
        
        view.addSubview(tableView)
        
        switchingTitle = UILabel(frame: CGRect(x: 80, y: 20, width: view.bounds.width - 160, height: 44))
        switchingTitle.text = "话题"
        switchingTitle.textAlignment = .center
        switchingTitle.font = UIFont.boldSystemFont(ofSize: 16)
        switchingTitle.textColor = UIColor.white
        switchingTitle.alpha = 0
        headView.addSubview(switchingTitle)
        
        let topicHeadView = UIView(frame: CGRect(x: 16, y: 64, width: view.bounds.width-32, height: 200-64))
        headView.addSubview(topicHeadView)
        
        let topicImg = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
        topicImg.backgroundColor = UIColor.gray
        topicHeadView.addSubview(topicImg)
        
        let topicTitle = UILabel(frame: CGRect(x: 100, y: 0, width: topicHeadView.bounds.width - 100, height: 24))
        topicTitle.text = "#话题#"
        topicTitle.textColor = UIColor.white
        topicHeadView.addSubview(topicTitle)
        
        let topicInfo = UILabel(frame: CGRect(x: 100, y: 32, width: topicHeadView.bounds.width - 100, height: 24))
        topicInfo.textColor = UIColor.white
        topicInfo.font = UIFont.systemFont(ofSize: 14)
        topicInfo.text = "粉丝: ___  讨论: ___"
        topicHeadView.addSubview(topicInfo)
        
        let followBtn = UIButton(frame: CGRect(x: topicHeadView.bounds.width - 80, y: topicHeadView.bounds.height - 40, width: 80, height: 32))
        followBtn.setTitle("关注话题", for: .normal)
        followBtn.setTitleColor(UIColor.white, for: .normal)
        followBtn.layer.borderColor = UIColor.white.cgColor
        followBtn.layer.borderWidth = 1
        followBtn.layer.cornerRadius = 4
        followBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        topicHeadView.addSubview(followBtn)
        
        // Do any additional setup after loading the view.
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        if scrollView.contentOffset.y > 200 - 64 {
            if switchingTitle.alpha == 0 {
                UIView.animate(withDuration: 0.3, animations: {
                    self.switchingTitle.alpha = 1
                })
            }
            
        }else{
            if switchingTitle.alpha == 1 {
                UIView.animate(withDuration: 0.3, animations: {
                    self.switchingTitle.alpha = 0
                })
            }
        }
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        if section == 0{
//            return 1
//        }
        if section == 0{
            return 20
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        if indexPath.section == 0 && indexPath.row == 0{
//            return 192
//        }
        return 44
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "item")
//        if indexPath.section == 0 && indexPath.row == 0{
//            cell.backgroundColor = UIColor.lightGray
//        }
        return cell
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func goBack(){
        _ = navigationController?.popViewController(animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
